﻿Imports System.Data.OleDb

Public Class AddArticle
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnAddArticle_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddArticle.Click
        Dim UserID As String
        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser
        UserID = MemUser.ProviderUserKey.ToString

        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid
        Dim newFileName As String = ""

        If fuPicture.HasFile Then
            newFileName = myGUID.ToString() + ".jpg"
            Dim picLocation = Request.MapPath("pictures") & "/" & newFileName
            fuPicture.SaveAs(picLocation)
        Else
            newFileName = "default.article.jpg"
        End If

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "Insert into tblArticles (Category,Title,Picture,Details,DateAdded,UserID) Values (@f1,@f2,@f3,@f4,@f5,@f6)"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", ddlCategory.SelectedValue)
            cmd.Parameters.AddWithValue("@f2", txtTitle.Text)
            cmd.Parameters.AddWithValue("@f3", "pictures/" & newFileName)
            cmd.Parameters.AddWithValue("@f4", txtDetails.Text)
            cmd.Parameters.AddWithValue("@f5", Date.Today)
            cmd.Parameters.AddWithValue("@f6", UserID)

            oleDbConn.Open()
            cmd.ExecuteNonQuery()

            Response.Redirect("AddProgressConfirmation.aspx")
    End Sub
End Class